<template>
    <div>
        <div class="book-item">
            <div class="book-img" @click="toDetail">
                <img :src="baseURL + props.goods.pic" alt="">
            </div>
            <div class="book-info">
                <div class="book-title">
                    <span @click="toDetail" style="cursor: pointer;">{{ props.goods.title }}</span>
                </div>
                <div class="book-view">
                    <div>
                        <el-icon>
                            <View />
                        </el-icon>
                        {{ props.goods.view }}
                    </div>
                    <div>
                        <el-icon>
                            <Coin />
                        </el-icon>
                        {{ props.goods.count }}库存
                    </div>
                    <div>
                        ￥{{ formatPrice(props.goods.price) }}
                    </div>
                </div>
                <div class="book-desc">
                    <span>{{ props.goods.descrption }}</span>
                </div>

                <div class="book-btn">
                    <el-button type="primary" plain @click="toDetail">查看详情</el-button>
                    <el-button type="warning" @click="addCollect">
                        <el-icon size="20"><Star /></el-icon>
                        收藏
                    </el-button>
                    <el-button type="primary" @click="addList">
                        <el-icon size="20">
                            <ShoppingCart />
                        </el-icon>
                        &nbsp;加入购物车
                    </el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { baseURL } from '@/config/baseURL';
import { View, ShoppingCart, Star, Coin } from '@element-plus/icons-vue'
import { create } from '@/api/collect'
import { create as borrowCreate } from '@/api/borrow'
import { formatPrice } from '@/utils/formatPrice'
import { useUserStore } from '@/stores/useUser'
import moment from 'moment'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const router = useRouter()

const props = defineProps(['goods'])

const toDetail = () => {
    router.push('/detail/' + props.goods.id)
}

//加入清单
const addList = () => {
    
    if(!userStore.isLogin){
        ElMessage.error('请先登录')
        return
    }

    borrowCreate({
        user_id: userStore.userinfo.id,
        goods_id: props.goods.id,
        day:1,
        status:0,
        create_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    }).then(res => {
        if (res.data.code == 200) {
            ElMessage.success(res.data.message)
        }
        else if (res.data.code == 300){
            ElMessage.warning(res.data.message)
        }else {
            ElMessage.error(res.data.message)
        }
    })
}

//加入收藏夹
const addCollect = () => {

if(!userStore.isLogin){
    ElMessage.error('请先登录')
    return
}

create({
    goods_id: props.goods.id,
    user_id: userStore.userinfo.id,
    create_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
    goods: props.goods.title
}).then(res => {
    if (res.data.code == 200) {
        ElMessage.success(res.data.message)
    }
    else if (res.data.code == 300){
        ElMessage.warning(res.data.message)
    }else {
        ElMessage.error(res.data.message)
    }
})
}


</script>

<style scoped lang='less'>
.book-item {
    width: 100%;
    height: 250px;
    background: #fff;
    box-shadow: 1px 1px 3px #39393925;
    border: 1px solid #efefef;
    border-radius: 5px;
    overflow: hidden;
    display: flex;
    margin: 20px 0;
    align-items: center;

    .book-img {
        width: 200px;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 5px 5px 0 0;
        flex-shrink: 0;
        box-shadow: 0 0 2px #00000033;
        cursor: pointer;

        img {
            width: 90%;
        }
    }

    .book-info {
        flex: 1;
        padding: 20px;
        box-sizing: border-box;


        .book-title {
            width: 100%;
            font-size: 18px;
            font-weight: bold;
            color: #555;
            display: flex;
            justify-content: space-between;

            .author {
                font-size: 14px;
                color: #999;
            }
        }
    }

    .book-view {
        margin-top: 20px;
        display: flex;
        color: #777;

        &>div {
            margin-right: 30px;
            display: flex;
            align-items: center;

            i {
                margin-right: 5px;
            }
        }
    }

    .book-desc {
        flex: 1;
        min-height: 70px;
        margin-top: 30px;
        color: #999;
    }

    .book-btn {
        text-align: end;
    }
}</style>